//
//  FDShopDetailController.m
//  FD
//
//  Created by Yasin on 2018/10/13.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FDShopDetailController.h"
//cell
#import "SDCouponsCell.h" // h : 150
#import "SDEvaluateCell.h" // 100；


//view
#import "FDShopDetailHeaderView.h"
#import "ShopDetailCellSectionView.h"
//model
#import "ShopHeaderViewModel.h"
#import "SDEvaluateModel.h"


//controller
#import "YuYueViewController.h" //立即预约页面


@interface FDShopDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tv;
/**优惠券数组*/
@property (nonatomic, strong) NSMutableArray *couponsArr;
/**评价数组*/
@property (nonatomic, strong) NSMutableArray *evaluateArr;
/**表头*/
@property (nonatomic,  strong) FDShopDetailHeaderView *headerV;
/**立即预约 按钮*/
@property (nonatomic, strong) UIButton *yuyueBtn;
@end

@implementation FDShopDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    [self setupView];
    //请求数据
    [self requestShopDetailData];
    [self requestShopOrderEvaluateListWithShopId:self.shopId];
    
//    [self loadLocalJSON];
}

- (void)setupView {
    NSString *shopDetail = NSLocalizedString(Home_ShopDetail, @"门店详情");
    self.navigationItem.title = shopDetail;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //2019-2-23 图片宽高比1:1
//    FDShopDetailHeaderView *headerV = [[FDShopDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 260)];
    FDShopDetailHeaderView *headerV = [[FDShopDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 110+SCREEN_WIDTH)];

    
    self.headerV = headerV;
    self.tv.tableHeaderView = self.headerV;
    self.tv.tableFooterView = [UIView new];
    //添加到view上
    [self.view addSubview:self.tv];
    [self.view addSubview:self.yuyueBtn];
    
    CGFloat tvHeight = (self.view.bounds.size.height - FD_TabbarSafeBottomMargin - 40);
    self.tv.frame = CGRectMake(0, 0, SCREEN_WIDTH, tvHeight);
    //设置约束
    [self.yuyueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark - getter
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
        _tv.delegate = self;
        _tv.dataSource = self;
        
        //注册cell
        [_tv registerNib:[UINib nibWithNibName:@"SDCouponsCell" bundle:nil] forCellReuseIdentifier:@"SDCouponsCell"];
        [_tv registerClass:[SDEvaluateCell class] forCellReuseIdentifier:@"SDEvaluateCell"];
        
        //取消线
        _tv.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tv;
}
- (NSMutableArray *)evaluateArr {
    if (!_evaluateArr) {
        _evaluateArr = [NSMutableArray array];
    }
    return _evaluateArr;
}


- (UIButton *)yuyueBtn {
    if (!_yuyueBtn) {
        //立即预约国际化字段
        NSString *lijiyuyueStr = NSLocalizedString(Home_LiJiYuYue, @"立即预约");
        _yuyueBtn = [UIButton createFastRedBtnRadius:0 target:self action:@selector(clickYuYueAction:) title:lijiyuyueStr];
    }
    return _yuyueBtn;
}

#pragma mark - 立即预约
- (void)clickYuYueAction:(UIButton *)btn
{
  //跳转预约模块。
    YuYueViewController *yuyueVC = [[YuYueViewController alloc] init];
    yuyueVC.shopId = self.shopId; //门店编号
    [self.navigationController pushViewController:yuyueVC animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.evaluateArr.count;
}

#pragma mark - 代理 UITableViewDelegate,UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
//    if (indexPath.section == 0) {
//        SDCouponsCell *couponsCell = [tableView dequeueReusableCellWithIdentifier:@"SDCouponsCell"];
//
//        return couponsCell;
//
//    }else {
//        SDEvaluateCell *evaluateCell = [tableView dequeueReusableCellWithIdentifier:@"SDEvaluateCell"];
//
//        SDEvaluateModel *evaluateM = self.evaluateArr[indexPath.row];
//        evaluateCell.evaluateModel = evaluateM;
//
//        return evaluateCell;
//    }
    
    SDEvaluateCell *evaluateCell = [tableView dequeueReusableCellWithIdentifier:@"SDEvaluateCell"];
    
    SDEvaluateModel *evaluateM = self.evaluateArr[indexPath.row];
    evaluateCell.evaluateModel = evaluateM;
    
    return evaluateCell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if(indexPath.section == 0) {
//        return 150;
//    }else {
//        SDEvaluateModel *model = self.evaluateArr[indexPath.row];
//        return model.cellHeight;
//    }
 
    SDEvaluateModel *model = self.evaluateArr[indexPath.row];
    return model.cellHeight;

}


//返回区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ShopDetailCellSectionView *sectionV = [[ShopDetailCellSectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    
//    if (section == 0) {
//        sectionV.lb.text = @"门店优惠券";
//    }else {
//        sectionV.lb.text = @"评价";
//    }
//    return sectionV;
    NSString *pjStr = NSLocalizedString(L_Order_Evaluate, @"评价");
    
    sectionV.lb.text = [NSString stringWithFormat:@"%@(%ld)",pjStr,self.evaluateArr.count];
    return sectionV;
}

//15225358788


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

//白干
//请求数据
- (void)requestShopDetailData {

    
    [FDHttpAPi requestSYShopDetailId:self.shopId successBlock:^(id responseObjc) {
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) { //请求成功
            NSDictionary *dataDic = responseObjc[@"data"];
            
            ShopHeaderViewModel *model = [ShopHeaderViewModel mj_objectWithKeyValues:dataDic];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.headerV.headerModel = model;
            });
            
        }else { //toast
            [JRToast showWithText:responseObjc[@"describe"]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}

- (void)requestShopOrderEvaluateListWithShopId:(NSNumber *)shopId {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    
    [FDHttpAPi requestUserShopOrderEvaluateListWithShopId:shopId successBlock:^(id responseObjc) {
        [hud hideAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            //这里有坑 可能是数组也可能是字典
            
            NSString *clsStr = NSStringFromClass([responseObjc[@"data"] class]);
            if (![clsStr isEqualToString:@"__NSDictionaryI"]) {
                FDLog(@"服务器返回的不是dic,防止奔溃直接返回");
                return ;
            }
            
            FDLog(@"将类转换为字符串:%@",NSStringFromClass([responseObjc[@"data"] class]));
            FDLog(@"1111111111111");
            
            //解析数据
            NSDictionary *dataDic = responseObjc[@"data"];

//            判断字典是为空
            if (![[dataDic allKeys] count]) {
                FDLog(@"字典为空");
                return ;
            }

            NSArray *evaluateList = dataDic[@"evaluateList"];

            for (NSDictionary *dic in evaluateList) {
                SDEvaluateModel *model = [SDEvaluateModel mj_objectWithKeyValues:dic];

                [self.evaluateArr addObject:model];
            }

            //刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tv reloadData];
            });

        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
        
    } fialBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        [hud hideAnimated:YES];
    }];
}


#pragma mark - 读取本地json  11-29 无效

- (void)loadLocalJSON {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"FDShopDetailData" ofType:@"json"];
    NSData *JSONData = [NSData dataWithContentsOfFile:path];
    
//    NSJSONReadingMutableContainers; //易读的
    
    NSError *err = nil;
    NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:&err];
    if (err) {
        FDLog(@"解析失败:%@",err);
    }
    FDLog(@"%@",responseDic);
    
    //解析数据
    NSDictionary *dataDic = responseDic[@"data"];
    
    NSArray *evaluateList = dataDic[@"evaluateList"];
    
    for (NSDictionary *dic in evaluateList) {
        SDEvaluateModel *model = [SDEvaluateModel mj_objectWithKeyValues:dic];
        
        [self.evaluateArr addObject:model];
    }
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tv reloadData];
    });
}


@end
